package cn.edu.swu.wn.servlet;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

// 注解配置：访问路径为 /time（Servlet 3.0+ 支持，无需在web.xml中配置）
@WebServlet("/time")
public class TimeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置响应内容类型为HTML
        response.setContentType("text/html;charset=UTF-8");

        // 获取当前时间并格式化
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String currentTime = sdf.format(now);

        // 向页面输出内容
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>当前时间</title></head>");
        out.println("<body>");
        out.println("<h1>服务器当前时间：" + currentTime + "</h1>");
        out.println("</body></html>");
    }
}
